﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SEP.Admin
{
    public partial class UnitTesting : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Log(string msg)
        {
            txbUTOutput.Text += Environment.NewLine + msg;
        }

        protected void btnRun_Click(object sender, EventArgs e)
        {
            Log("Running tests...");
            if (RunUnitTests())
            {
                Log("Success");
            }
            else
            {
                Log("Failed");
            }
        }

        protected Boolean RunUnitTests()
        {
            Boolean utCountry = false;
            Boolean utState = false;
            Boolean utEmailSend = false;

            try
            {
                Log("BEGIN: Country Unit Tests");
                utCountry = UnitTestCountry();
                Log("END: Country Unit Tests");
                Log("--------------------");
                Log("--------------------");
                Log("BEGIN: State Unit Tests");
                utState = UnitTestState();
                Log("END: State Unit Tests");
                Log("--------------------");
                Log("--------------------");
                Log("BEGIN: Email Unit Tests");
                utEmailSend = UnitTestEmailSend();
                Log("END: Email Unit Tests");
                Log("--------------------");
            }
            catch (Exception ex)
            {
                Log("Exception: " + ex.Message);
            }

            return utCountry && utState && utEmailSend;
        }

        protected Boolean UnitTestState()
        {
            try
            {
                string TestDescription = "North Carolina";
                string TestAbbreviation = "NC";
                string ErrorMessageTemplate = "{0} was '{1}', expected '{2}'";
                Boolean AnyFailures = false;
                Guid createdID = Guid.Empty;
                try
                {
                    //create a new State
                    BusinessLogic.State createdState = new BusinessLogic.State();
                    createdState.Description = TestDescription;
                    createdState.Abbreviation = TestAbbreviation;
                    createdState.Create();
                    createdID = createdState.ID;
                    if (createdID != Guid.Empty)
                    {
                        Log("Created successfully");
                    }
                }
                catch (Exception StateException)
                {
                    Log("Exception while trying to create State: " + StateException.Message);
                    AnyFailures = true;
                }

                Log("Retrieving State...");
                BusinessLogic.State retrievedState = null;
                Boolean LoadMismatches = false;
                try
                {
                    //verify that we can pull it back
                    retrievedState = new BusinessLogic.State(createdID);
                    Log("Record retrieved");
                    if (retrievedState.Description != TestDescription)
                    {
                        LoadMismatches = true;
                        Log(string.Format(ErrorMessageTemplate, "Description", retrievedState.Description, TestDescription));
                    }
                    else
                    {
                        Log("Description loaded correctly");
                    }
                    if (retrievedState.Abbreviation != TestAbbreviation)
                    {
                        LoadMismatches = true;
                        Log(string.Format(ErrorMessageTemplate, "Abbreviation", retrievedState.Abbreviation, TestAbbreviation));
                    }
                    else
                    {
                        Log("Abbreviation loaded correctly");
                    }
                }
                catch (Exception retrieveException)
                {
                    Log("Exception while trying to retrieve State: " + retrieveException.Message);
                    AnyFailures = true;
                }
                AnyFailures = AnyFailures || LoadMismatches;
                Log("Retrieve test complete");

                Log("Deleting State...");
                try
                {
                    //delete it
                    retrievedState.Delete();
                    Log("Delete complete");

                    //verify it is gone
                    retrievedState = new BusinessLogic.State(createdID);
                    if (retrievedState.ID != Guid.Empty)
                    {
                        Log(string.Format(ErrorMessageTemplate, "State", "there", "it to be deleted"));
                        AnyFailures = true;
                    }
                    else
                    {
                        Log("Verified delete");
                    }
                }
                catch (Exception deleteException)
                {
                    Log("Exception while trying to delete State: " + deleteException.Message);
                    AnyFailures = true;
                }

                //no failures means everything passed
                return !AnyFailures;
            }
            catch (Exception ex)
            {
                Log("Error while testing countries: " + ex.Message);
                return false;
            }
        }

        protected Boolean UnitTestCountry()
        {
            try
            {
                string TestDescription = "Japan";
                string TestAbbreviation = "JAP";
                string ErrorMessageTemplate = "{0} was '{1}', expected '{2}'";
                Boolean AnyFailures = false;
                Guid createdID = Guid.Empty;
                try
                {
                    //create a new country
                    BusinessLogic.Country createdcountry = new BusinessLogic.Country();
                    createdcountry.Description = TestDescription;
                    createdcountry.Abbreviation = TestAbbreviation;
                    createdcountry.Create();
                    createdID = createdcountry.ID;
                    if (createdID != Guid.Empty)
                    {
                        Log("Created successfully");
                    }
                }
                catch (Exception countryException)
                {
                    Log("Exception while trying to create country: " + countryException.Message);
                    AnyFailures = true;
                }

                Log("Retrieving country...");
                BusinessLogic.Country retrievedcountry = null;
                Boolean LoadMismatches = false;
                try
                {                    
                    //verify that we can pull it back
                    retrievedcountry = new BusinessLogic.Country(createdID);
                    Log("Record retrieved");
                    if (retrievedcountry.Description != TestDescription)
                    {
                        LoadMismatches = true;
                        Log(string.Format(ErrorMessageTemplate, "Description", retrievedcountry.Description, TestDescription));
                    }
                    else
                    {
                        Log("Description loaded correctly");
                    }
                    if (retrievedcountry.Abbreviation != TestAbbreviation)
                    {
                        LoadMismatches = true;
                        Log(string.Format(ErrorMessageTemplate, "Abbreviation", retrievedcountry.Abbreviation, TestAbbreviation));
                    }
                    else
                    {
                        Log("Abbreviation loaded correctly");
                    }
                }
                catch (Exception retrieveException)
                {
                    Log("Exception while trying to retrieve country: " + retrieveException.Message);
                    AnyFailures = true;
                }
                AnyFailures = AnyFailures || LoadMismatches;
                Log("Retrieve test complete");

                Log("Deleting country...");
                try
                {
                    //delete it
                    retrievedcountry.Delete();
                    Log("Delete complete");

                    //verify it is gone
                    retrievedcountry = new BusinessLogic.Country(createdID);
                    if (retrievedcountry.ID != Guid.Empty)
                    {
                        Log(string.Format(ErrorMessageTemplate, "Country", "there", "it to be deleted"));
                        AnyFailures = true;
                    }
                    else
                    {
                        Log("Verified delete");
                    }
                }
                catch (Exception deleteException)
                {
                    Log("Exception while trying to delete country: " + deleteException.Message);
                    AnyFailures = true;
                }

                //no failures means everything passed
                return !AnyFailures;
            }
            catch (Exception ex)
            {
                Log("Error while testing countries: " + ex.Message);
                return false;
            }
        }

        protected Boolean UnitTestEmailSend()
        {
            try
            {
                Communications.EmailSender emailSender = new Communications.EmailSender();
                emailSender.FromEmailAddress = "joseph.styons@gmail.com";
                emailSender.EmailServerName = "smtp.gmail.com";
                emailSender.AttachmentFiles.Add(@"C:\\dev\\src\\codeplex\\palmettoconsulting\\southeast pathology phase 1\\development\\source\\SEP\\Admin\\UnitTesting.aspx.cs");
                emailSender.AttachmentFiles.Add(@"C:\\dev\\src\\codeplex\\palmettoconsulting\\southeast pathology phase 1\\development\\source\\SEP\\Code\\Communications\\Email.cs");
                emailSender.FromEmailAccountPassword = "somepassword";
                emailSender.FromEmailAccountUsername = "jstyons@gmail.com";                
                emailSender.MessageBody = "Hey man," + Environment.NewLine
                    + "It looks like email sending is working now." + Environment.NewLine
                    + "The code is attached as 'Email.cs', and will be checked in shortly." + Environment.NewLine
                    + "On the front end, I put this under the 'Unit Tests' area." + Environment.NewLine
                    + "Example usage is inside the UnitTesting.aspx.cs attachment." + Environment.NewLine
                    + "Have a good one, tty tomorrow." + Environment.NewLine
                    + "couple of things to note:" + Environment.NewLine
                    + "1 - The format of the Email is HTML" + Environment.NewLine
                    + "2 - It supports attachments and SSL";
                emailSender.MessageSubject = "Email is working";
                emailSender.RecipientEmailAddresses.Add("joseph.styons@blackbaud.com");
                emailSender.RecipientEmailAddresses.Add("chris.whisenhunt@blackbaud.com");
                emailSender.SendEmail();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}